// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.chat.defaultChats;

import java.nio.ByteBuffer;
import zombie.Lua.LuaEventManager;
import zombie.Lua.LuaManager;
import zombie.characters.IsoPlayer;
import zombie.chat.ChatMessage;
import zombie.chat.ChatMode;
import zombie.chat.ChatSettings;
import zombie.chat.ChatTab;
import zombie.core.Color;
import zombie.core.math.PZMath;
import zombie.core.network.ByteBufferWriter;
import zombie.network.chat.ChatType;
import zombie.radio.ZomboidRadio;
import zombie.radio.devices.DeviceData;
import zombie.ui.UIFont;

public class RadioChat extends RangeBasedChat {
    public RadioChat(ByteBuffer bb, ChatTab tab, IsoPlayer owner) {
        super(bb, ChatType.radio, tab, owner);
        if (!this.isCustomSettings()) {
            this.setSettings(getDefaultSettings());
        }

        this.customTag = "radio";
    }

    public RadioChat(int id, ChatTab tab) {
        super(id, ChatType.radio, tab);
        if (!this.isCustomSettings()) {
            this.setSettings(getDefaultSettings());
        }

        this.customTag = "radio";
    }

    public RadioChat() {
        super(ChatType.radio);
        this.setSettings(getDefaultSettings());
        this.customTag = "radio";
    }

    public static ChatSettings getDefaultSettings() {
        ChatSettings chatSettings = new ChatSettings();
        chatSettings.setBold(true);
        chatSettings.setFontColor(Color.lightGray);
        chatSettings.setShowAuthor(false);
        chatSettings.setShowChatTitle(true);
        chatSettings.setShowTimestamp(true);
        chatSettings.setUnique(true);
        chatSettings.setAllowColors(true);
        chatSettings.setAllowFonts(false);
        chatSettings.setAllowBBcode(true);
        chatSettings.setAllowImages(false);
        chatSettings.setAllowChatIcons(true);
        return chatSettings;
    }

    @Override
    public ChatMessage createMessage(String text) {
        ChatMessage chatMessage = super.createMessage(text);
        if (this.getMode() == ChatMode.SinglePlayer) {
            chatMessage.setOverHeadSpeech(true);
            chatMessage.setShowInChat(false);
        }

        chatMessage.setShouldAttractZombies(true);
        return chatMessage;
    }

    public ChatMessage createBroadcastingMessage(String text, int channel) {
        ChatMessage chatMessage = super.createBubbleMessage(text);
        chatMessage.setAuthor("");
        chatMessage.setShouldAttractZombies(false);
        chatMessage.setRadioChannel(channel);
        return chatMessage;
    }

    public ChatMessage createStaticSoundMessage(String text) {
        ChatMessage chatMessage = super.createBubbleMessage(text);
        chatMessage.setAuthor("");
        chatMessage.setShouldAttractZombies(false);
        return chatMessage;
    }

    @Override
    protected void showInSpeechBubble(ChatMessage chatMessage) {
        Color color = this.getColor();
        this.getSpeechBubble()
            .addChatLine(
                chatMessage.getText(),
                color.r,
                color.g,
                color.b,
                UIFont.Dialogue,
                this.getRange(),
                this.customTag,
                this.isAllowBBcode(),
                this.isAllowImages(),
                this.isAllowChatIcons(),
                this.isAllowColors(),
                this.isAllowFonts(),
                this.isEqualizeLineHeights()
            );
    }

    @Override
    public void showMessage(ChatMessage msg) {
        if (this.isEnabled() && msg.isShowInChat() && this.hasChatTab()) {
            LuaEventManager.triggerEvent("OnAddMessage", msg, this.getTabID());
        }
    }

    @Override
    public void sendToServer(ChatMessage msg, DeviceData deviceData) {
        if (deviceData != null) {
            int int0 = PZMath.fastfloor(this.getChatOwner().getX());
            int int1 = PZMath.fastfloor(this.getChatOwner().getY());
            int int2 = deviceData.getTransmitRange();
            ZomboidRadio.getInstance().SendTransmission(int0, int1, msg, int2);
        }
    }

    @Override
    public ChatMessage unpackMessage(ByteBuffer bb) {
        ChatMessage chatMessage = super.unpackMessage(bb);
        chatMessage.setRadioChannel(bb.getInt());
        chatMessage.setOverHeadSpeech(bb.get() == 1);
        chatMessage.setShowInChat(bb.get() == 1);
        chatMessage.setShouldAttractZombies(bb.get() == 1);
        return chatMessage;
    }

    @Override
    public void packMessage(ByteBufferWriter b, ChatMessage msg) {
        super.packMessage(b, msg);
        b.putInt(msg.getRadioChannel());
        b.putBoolean(msg.isOverHeadSpeech());
        b.putBoolean(msg.isShowInChat());
        b.putBoolean(msg.isShouldAttractZombies());
    }

    @Override
    public String getMessagePrefix(ChatMessage msg) {
        StringBuilder stringBuilder = new StringBuilder(this.getChatSettingsTags());
        if (this.isShowTimestamp()) {
            stringBuilder.append("[").append(LuaManager.getHourMinuteJava()).append("]");
        }

        if (this.isShowTitle()) {
            stringBuilder.append("[").append(this.getTitle()).append("]");
        }

        if (this.isShowAuthor() && msg.getAuthor() != null && !msg.getAuthor().equals("")) {
            stringBuilder.append(" ").append(msg.getAuthor()).append(" ");
        } else {
            stringBuilder.append(" ").append("Radio").append(" ");
        }

        stringBuilder.append(" (").append(this.getRadioChannelStr(msg)).append("): ");
        return stringBuilder.toString();
    }

    private String getRadioChannelStr(ChatMessage chatMessage) {
        StringBuilder stringBuilder = new StringBuilder();
        int int0 = chatMessage.getRadioChannel();
        int int1 = int0 % 1000;

        while (int1 % 10 == 0 && int1 != 0) {
            int1 /= 10;
        }

        int int2 = int0 / 1000;
        stringBuilder.append(int2).append(".").append(int1).append(" MHz");
        return stringBuilder.toString();
    }
}
